import $V from './$V';

export default function cr(high, low, close, length) {
    let tp = $V.div_constant($V.add($V.add(high, low), close), 3);
    let crValue = $V.init(high.length, 0);
    crValue = $V.set_by_index(crValue, $V.range(length), 100);
    for(let i = length; i < high.length; i++) {
        let tmp1 = $V.sub($V.subv(high, i - length + 1, i + 1), $V.subv(tp, i - length, i));
        tmp1 = $V.set_by_index(tmp1, $V.where(tmp1, '<', 0), 0);
        let tmp2 = $V.sub($V.subv(tp, i - length, i), $V.subv(low, i - length + 1, i + 1));
        tmp2 = $V.set_by_index(tmp2, $V.where(tmp2, '<', 0), 0);
        crValue[i] = $V.sum(tmp1) / $V.sum(tmp2) * 100;
    }
    return crValue;
}
